package com.lzq.badminton.security;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

@Getter
@Setter
@ToString
public class WxAuthenticationToken extends AbstractAuthenticationToken {

    private String openId;
    private Long userId;
    private String name;
    private String avatar;
    private String phone;

    public WxAuthenticationToken(String openId, String name, String avatar, String phone) {
        super(null);
        this.openId = openId;
        this.name = name;
        this.avatar = avatar;
        this.phone = phone;
    }


    // 使用openid创建一个已验证的token
    public WxAuthenticationToken(String openId, Long userId, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.openId = openId;
        this.userId = userId;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.openId;
    }

    @Override
    public Object getPrincipal() {
        return this.userId;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }


}
